package com.example.smart.adapter.other

import android.app.Activity
import android.view.LayoutInflater
import android.view.ViewGroup
import com.example.lib.extend.gone
import com.example.lib.extend.isVisi
import com.example.lib.extend.loadImage
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.utils.image_selector.ImageSelector
import com.example.smart.base.adapter.BaseRecyclerAdapter
import com.example.smart.base.adapter.BaseViewHolder
import com.example.smart.databinding.ItemChooseImageBinding
import com.example.smart.R
import java.util.LinkedList
import kotlin.collections.filter
import kotlin.collections.first
import kotlin.collections.last
import kotlin.collections.none
import kotlin.collections.toMutableList
import kotlin.text.isEmpty
import kotlin.text.isNotEmpty

class ChooseImageAdapter(val activity: Activity, private val maxSize: Int = 1) :
    BaseRecyclerAdapter<ItemChooseImageBinding, String>(activity, LinkedList<String>()) {
    var isCanEdit = true

    init {
        add("", 0)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<ItemChooseImageBinding> {
        return BaseViewHolder(ItemChooseImageBinding.bind(LayoutInflater.from(context)
            .inflate(R.layout.item_choose_image, parent, false)))
    }

    override fun onBindView(itemView: ItemChooseImageBinding, bean: String, position: Int) {
        if (bean.isEmpty()) {
            itemView.ItemImage.setImageResource(com.example.lib.R.mipmap.icon_add_image)
            itemView.ItemRemove.gone()
        } else {
            itemView.ItemImage.loadImage(bean)
            itemView.ItemRemove.isVisi(isCanEdit)
        }

        itemView.ItemImage.setOnDoubleClickListener {
            if (isCanEdit) {
                if (bean.isEmpty()) {
                    takePhoto()
                }
            }
        }

        itemView.ItemRemove.setOnDoubleClickListener {
            remove(position)
        }
    }

    private fun takePhoto() {
        ImageSelector.takePhoto(context) {
            val first = it.first()
            if (getList().filter { fil -> fil.isNotEmpty() }.size < maxSize) {
                add(first.realPath, 0)
            }

            if (getList().filter { fil -> fil.isNotEmpty() }.size == maxSize) {
                remove(size() - 1)
            }
        }
    }

    override fun remove(position: Int, isNull: () -> Unit) {
        super.remove(position, isNull)
        if (size() < maxSize && getList().none { fil -> fil.isEmpty() }) {
            add("", size())
        }
    }

    override fun removeAll() {
        super.removeAll()
        add("", 0)
    }

    fun updateList(list: MutableList<String>, isCanEdit: Boolean) {
        super.updateList(list)
        this.isCanEdit = isCanEdit
        if (isCanEdit && list.size < maxSize && getList().last().isNotEmpty()) {
            add("")
        }
    }

    fun getImageList(): MutableList<String> {
        return getList().filter { fil -> fil.isNotEmpty() }.toMutableList()
    }
}